<?php

// +----------------------------------------------------------------------
// | 用户留言
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\event\ucenter;

use app\model\ucenter\Feedback as FeedbackModel;
use app\model\app\JdOrder as JdOrderModel;
use app\model\app\TbkOrder as TbkOrderModel;
use app\event\app\Tbk as TbkEvent;
use app\event\app\Jd as JdEvent;
use app\common\event\Base;
use app\model\ucenter\User as UserModel;
use app\event\ucenter\Notice as NoticeEvent;
use think\Exception;

class Feedback extends Base
{

    /**
     * 提交意见反馈
     * @param array $param
     * @param UserModel $user
     * @return array
     */
    public function add(array $param, $user): array
    {
        try {
            $images = [];
//				if (isset($param['images'])) {
//					foreach ($param['images'] as $image) {
//						$images[] = $image['id'];
//					}
//					$param['images'] = $images;
//				}
            $insert['user_id'] = $user['id'];
            $insert['type'] = 1;
            $insert['types'] = 1;
            $insert['title'] = $param['title'];
            $insert['content'] = $param['content'];
            $insert['images'] = empty($param['images']) ? [] : explode(',', $param['images']);
            $insert['update_time'] = time_format();
//				$insert['mobile'] = $param['mobile'];
            $feedback = FeedbackModel::create($insert);
            if (!$feedback) {
                throw new Exception('系统故障，请稍候再试~');
            }
            if ($param['title'] == 'tbk') {
                $feedback['title'] = '淘宝客补单';
                $order = TbkOrderModel::where('trade_id', trim($param['content']))->find();
                if ($order) {
                    if ($order['user_id']) {
                        if ($order['user_id'] == $user['id'])
                        {
                            $feedback['is_end'] = 1;
                            $feedback['reply_time'] = time_format();
                            $data['data'] = '您已经绑定，请勿重复提交';
                            $data['message'] = '您已经绑定，请勿重复提交';
                        }else{
                            if ($param['images']) {
                                $data['data'] = '当前订单已经被绑定，您已经提交凭证，请等待处理';
                                $data['message'] = '当前订单已经被绑定，您已经提交凭证，请等待处理';
                            } else {
                                $data['data'] = '当前订单已经被绑定，建议您重新下单或者提交凭证';
                                $data['message'] = '当前订单已经被绑定，建议您重新下单或者提交凭证';
                                $feedback['is_end'] = 1;
                                $feedback['reply_time'] = time_format();
                            }
                        }

                        $data['code'] = 0;
                    } else {
                        // 绑定
                        $event = new TbkEvent();
                        $res = $event->supplement($order, $user);
                        if ($res['code'] == 0) {
                            $feedback['is_end'] = 1;
                            $feedback['reply_time'] = time_format();
                            $data['data'] = '补单成功';
                            $data['message'] = '补单成功';
                        } else {
                            $data['data'] = '补单异常，已经转入人工处理';
                            $data['message'] = '补单异常，已经转入人工处理';
                        }
                    }
                } else {

                    $feedback['is_end'] = 1;
                    $feedback['reply_time'] = time_format();
                    $data['code'] = 0;
                    $data['data'] = '订单号没有查询到，建议您取消订单重新购买';
                    $data['message'] = '订单号没有查询到，建议您取消订单重新购买';
                }
                $feedback->save();
            } elseif ($param['title'] == 'jd') {
                $feedback['title'] = '京东联盟补单';
                $order = JdOrderModel::where('order_id', trim($param['content']))->find();
                if ($order) {

                    $data['code'] = 0;
                    if ($order['user_id']) {
                        if ($order['user_id'] == $user['id'])
                        {
                            $feedback['is_end'] = 1;
                            $feedback['reply_time'] = time_format();

                            $data['data'] = '您已经绑定，请勿重复提交';
                            $data['message'] = '您已经绑定，请勿重复提交';
                        }else{
                            if ($param['images']) {
                                $data['data'] = '当前订单已经被绑定，您已经提交凭证，请等待处理';
                                $data['message'] = '当前订单已经被绑定，您已经提交凭证，请等待处理';
                            } else {
                                $data['data'] = '当前订单已经被绑定，建议您重新下单或者提交凭证';
                                $data['message'] = '当前订单已经被绑定，建议您重新下单或者提交凭证';
                                $feedback['is_end'] = 1;
                                $feedback['reply_time'] = time_format();
                            }
                        }

                    } else {
                        // 绑定
                        $event = new JdEvent();
                        $res = $event->supplement($order, $user);
                        if ($res['code'] == 0) {
                            $feedback['is_end'] = 1;
                            $feedback['reply_time'] = time_format();
                            $data['data'] = '补单成功';
                            $data['message'] = '补单成功';
                        } else {
                            $data['data'] = '补单异常，已经转入人工处理';
                            $data['message'] = '补单异常，已经转入人工处理';
                        }
                    }

                } else {

                    $feedback['is_end'] = 1;
                    $feedback['reply_time'] = time_format();
                    $data['code'] = 0;
                    $data['data'] = '订单号没有查询到，建议您取消订单重新购买';
                    $data['message'] = '订单号没有查询到，建议您取消订单重新购买';
                }
                $feedback->save();
            } else {
                $notice = [];
                $notice['type'] = 'feedback_add';
                $notice['title'] = '您的信息我们已经收到';
                $notice['content'] = '感谢您的反馈，后续可能会跟您联系！';
                NoticeEvent::send($notice, $user);
                $data['code'] = 0;
                $data['data'] = '您的信息我们已经收到，感谢您的反馈，后续可能会跟您联系！';
                $data['message'] = '您的信息我们已经收到，感谢您的反馈，后续可能会跟您联系！';
            }

        } catch (Exception $e) {
            $data['message'] = $e->getMessage();
            $data['code'] = 1;
        }
        return $data;
    }


    /**
     * 提交意见反馈
     * @param array $param
     * @param UserModel $user
     * @return array
     */
    public function reply(array $param, $user): array
    {
        try {
            $feedback = FeedbackModel::where('id', $param['id'])->find();
            if (!$feedback) {
                throw new Exception('系统故障，请稍候再试~');
            }
            $feedback['reply'] = $param['reply'];
            $feedback['is_end'] = 1;
            $feedback->save();
            $notice = [];
            $notice['type'] = 'feedback_reply';
            $notice['title'] = '您的反馈有新回复';
            $notice['content'] = '回复关于：' . $feedback['title'] . '的回复：' . $feedback['reply'];
            NoticeEvent::send($notice, $feedback->user);
            $data['code'] = 0;
            $data['message'] = '回复成功';
        } catch (Exception $e) {
            $data['message'] = $e->getMessage();
            $data['code'] = 1;
        }
        return $data;
    }


}
